﻿using Dapper;
using log4net;
using StoreProject.DBUtil;
using StoreProject.Entity.SettingEntity;
using StoreProject.Entity.StackEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreProject.DAL.StackDAL
{
    public class ProductDAL
    {
        ILog _log = LogManager.GetLogger("ProductDAL");

        public ProductDAL()
        {

        }
        public IDbConnection Conn
        {
            get
            {
                return MySqlHelper.Conn;
            }
        }

        public int? Insert(ProductEntity model)
        {
            try
            {
                StringBuilder sql = new StringBuilder("INSERT INTO Product(Id,ProductName,ProductCode,CreateTime,UpdateTime)");
                sql.Append(" VALUES( @Id,@ProductName,@ProductCode,@CreateTime,@UpdateTime)");
                using (Conn)
                {
                    int insert_Result = this.Conn.Execute(sql.ToString(), model);
                    return insert_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Insert", ex);
                return 0;
            }
        }

        public int Update(ProductEntity model)
        {
            try
            {
                using (this.Conn)
                {
                    int update_Result = this.Conn.Update(model);
                    return update_Result;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Update", ex);
                return 0;
            }
        }

        public ProductEntity Get(string id)
        {
            ProductEntity model = null;
            try
            {
                using (this.Conn)
                {
                    model = this.Conn.Get<ProductEntity>(id);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Get", ex);
            }
            return model;
        }

        public bool IsExists(string id)
        {
            bool isExist = false;
            using (this.Conn)
            {
                ProductEntity model = Get(id);
                isExist = (model == null) ? false : true;
            }
            return isExist;
        }

        public int Delete(string id)
        {
            using (Conn)
            {
                ProductEntity model = new ProductEntity();
                model.Id = id;
                return Conn.Delete(model);
            }
        }

        public int Delete(ProductEntity model)
        {
            using (Conn)
            {
                return Conn.Delete(model);
            }
        }

        public IEnumerable<ProductEntity> GetList()
        {
            using (Conn)
            {
                return Conn.GetList<ProductEntity>();
            }
        }

        public IEnumerable<ProductEntity> GetListEx()
        {
            using (Conn)
            {
                string sql = "select * from Product";
                return Conn.Query<ProductEntity>(sql);
            }
        }

        public IEnumerable<ProductEntity> GetListByWhere(string strWhere)
        {
            try
            {
                using (Conn)
                {
                    string sql = "select * from Product";
                    if (!string.IsNullOrEmpty(strWhere))
                    {
                        sql += " where " + strWhere;
                    }
                    return Conn.Query<ProductEntity>(sql);
                }
            }
            catch (Exception ex)
            {
                _log.Error("GetListByWhere", ex);
                return null;
            }
        }


        public int DeleteAll()
        {
            using (Conn)
            {
                string sql = "delete from Product";
                return Conn.Execute(sql);
            }
        }
        public ProductEntity GetById(string id)
        {
            try
            {
                using (Conn)
                {
                    string sql = "select * from Product where id=@id ";
                    return Conn.QueryFirstOrDefault<ProductEntity>(sql, new { id = id });
                }
            }
            catch (Exception ex)
            {
                _log.Error("GetById", ex);
                return null;
            }
        }
    }
}
